
import apiType, {
    GET_STORE_INFO,
    GET_STORE_LIST,
    GET_STORE_SELECTION,
} from "@constants/ActionTypes";
import {arrayToObject} from "@utils/arrayHelper";

const initialState = {
    storeList: [],
    storeSelection: [], // 门店下拉列表
    storeMap: {},
    storeInfoMap: {}
}

export default function update(state = initialState, action) {
    const { response, params, payload } = action
    switch (action.type) {
        case apiType(GET_STORE_SELECTION).success:
            return {
                ...state,
                storeSelection: response.stores,
                storeMap: {
                    ...state.storeMap,
                    ...arrayToObject(response.stores, 'id')
                }
            }
        case apiType(GET_STORE_LIST).success:
            return {
                ...state,
                storeList: response.stores,
                storeMap: {
                    ...state.storeMap,
                    ...arrayToObject(response.stores, 'id')
                }
            }
        case apiType(GET_STORE_INFO).success:
            return {
                ...state,
                storeInfoMap: {
                    ...state.storeInfoMap,
                    [params.store_id]: response
                }
            }
        default:
            return state
    }
}